Commonly Used Pseudo-Elements in CSS
Pseudo-elements allow you to style specific parts of an element or insert content without modifying the HTML. Some pseudo-elements are widely used for text formatting and decorative purposes.
::before - Inserts content before an element's content.
::after - Inserts content after an element's content.
::first-letter - Styles the first letter of a block-level element.
::first-line - Styles the first line of a block-level element.
::selection - Styles the portion of text selected by the user.
::placeholder - Styles the placeholder text of input elements.
::marker - Styles the marker of list items (li).
In this example, ::first-letter styles the first letter, ::before and ::after add decorative content, ::placeholder styles the input placeholder, and ::selection changes the appearance of selected text.
Use pseudo-elements for decorative or structural styling without extra HTML.
Combine them with CSS properties like content, color, font-size, and background.
Test across browsers to ensure consistent behavior.
Avoid using pseudo-elements for interactive content that requires JavaScript functionality.
How would you add a star icon before every review rating using CSS without changing the HTML?
What happens if you try to style ::content or ::text — why won’t it work?
You’re trying to add a tooltip arrow using ::after, but it’s not showing up — what’s the most likely missing CSS rule?
A designer wants a decorative border around a quote block using ::before and ::after, but it’s breaking on mobile — how would you debug this?
Your team’s CSS library uses ::before for icons, but now accessibility testers say screen readers are reading the content — how do you fix that without removing the visual effect?
A button’s ::after hover effect works in Chrome but not Safari — what’s your debugging process?
You’re building a scalable component library where pseudo-elements are used for state indicators — how do you ensure they don’t conflict with user-defined content or break in shadow DOM?
A legacy component uses ::before to inject loading spinners via CSS content — but now we’re migrating to a framework that dynamically renders content. How do you refactor this without breaking existing styles?
How would you optimize a UI with dozens of elements using ::before/::after for icons, and what performance tradeoffs should you consider in a high-traffic app?
Your company’s design system relies heavily on pseudo-elements for visual states across 50+ components — how do you architect a migration to CSS custom properties or a modern alternative without breaking thousands of downstream usages?
Pseudo-elements are used for accessibility-enhanced tooltips in a legacy enterprise app, but they’re causing reflow issues on low-end devices. How do you propose a long-term architectural shift while maintaining backward compatibility?
How would you evaluate whether to deprecate pseudo-element-based UI patterns in favor of SVG or web components in a large codebase with multiple teams and no centralized style governance?